package com.cgx.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author chenguanxin
 * @date 2021-05-09 22:52
 */
@Component
@Aspect
public class ResultHandlerAop {

    @Around("@annotation(com.cgx.annotation.ResultHandler)")
    public Object resultHandNotify(ProceedingJoinPoint jp){
        HashMap<String, Object> result = new HashMap<>();
        try {
            Object data = jp.proceed();
            result.put("code", 0);
            result.put("msg", "操作成功");
            result.put("success", true);
            result.put("data", data);
            return result;
        } catch (IllegalArgumentException throwable) {
            throwable.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败:" + throwable.getMessage());
            result.put("success", false);
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败");
            result.put("success", false);
            return result;
        }
    }
}
